package com.gxyk.tool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.widget.Toast;
public class RegexMobile {
	
	/**
	 * 验证手机号是否合法
	 * @param mobileNumber 要验证的手机号
	 * @return   true代表验证成功,false代表验证失败
	 */
	public static boolean VildateMobile(String mobileNumber)
	{
		String regex="^1[3458]\\d{9}$";
		Pattern pattern=Pattern.compile(regex);
		Matcher matcher= pattern.matcher(mobileNumber);
		return matcher.matches();
		
	}
	
	
	public static void main(String[] args)
	{
		System.out.println(VildateMobile("13141403201"));
	}
	
	//用于判断手机号
	/*if(TextUtils.isEmpty(phone)||"".equals(phone)){
		Toast.makeText(getApplicationContext(), "手机号不能为空", 0).show();
		return;
	}
	if(!RegexMobile.VildateMobile(phone)){
		Toast.makeText(getApplicationContext(), "输入的手机号不合法", 0).show();
		return;
	}*/
	
	
	        //短信回填（用于验证码）
/*	用于定义
 * private BroadcastReceiver smsReceiver;// 定义一个广播接收器
	private IntentFilter filter2;//
	private String strContent; // 验证码内容
	private String patternCoder = "(?<!\\d)\\d{4,10}(?!\\d)";// 验证短信内容
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			//verify_code_et.setText(strContent); //验证码文本框
		};
	}
	*/
	/*  用于oncreat()   <uses-permission android:name="android.permission.RECEIVE_SMS"/>
	filter2 = new IntentFilter();// 创建意图过滤器
	filter2.addAction("android.provider.Telephony.SMS_RECEIVED");// 创建意图动作
	filter2.setPriority(Integer.MAX_VALUE);// 设置等级最大
	
	 smsReceiver = new BroadcastReceiver() {
	
		@Override
		public void onReceive(Context context, Intent intent) {
			Object[] objs = (Object[]) intent.getExtras().get("pdus");// 获取收到的消息
			for (Object obj : objs) {
				byte[] pdu = (byte[]) obj;
				SmsMessage sms = SmsMessage.createFromPdu(pdu);
				// 短信的内容
				String message = sms.getMessageBody();// 获取手机短信的内容
				// 短息的手机号。。+86开头？
				String from = sms.getOriginatingAddress();// 获取手机号码
//				Toast.makeText(getApplicationContext(), "from: " + from,
//						Toast.LENGTH_LONG).show();
				if (!TextUtils.isEmpty(from)) {// 判断号码是否为空
					String code = patternCode(message);
					if (!TextUtils.isEmpty(code)) {
						strContent = code;
						handler.sendEmptyMessage(1);
					}
				}
			}
			
		}
	};
	registerReceiver(smsReceiver, filter2);// 注册广播监听器

}

@Override
protected void onDestroy() {
	super.onDestroy();
	// 解除自动输入的广播
	unregisterReceiver(smsReceiver);
}

/**
 * 匹配短信中间的6个数字（验证码等）
 * 
 * @param patternContent
 * @return
 */
	/*
private String patternCode(String patternContent) {
	if (TextUtils.isEmpty(patternContent)) {
		return null;
	}
	Pattern p = Pattern.compile(patternCoder);
	Matcher matcher = p.matcher(patternContent);
	if (matcher.find()) {
		return matcher.group();
	}
	return null;
}*/
}
